package dao;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class BaseDao <T> {
	private Class<T> entityClass;
	
	@PersistenceContext(unitName="EnrollmentManagement-PU")
	protected EntityManager em;

	@SuppressWarnings("unchecked")
	public BaseDao() {
		ParameterizedType supperClass = (ParameterizedType) getClass().getGenericSuperclass();
		this.entityClass = (Class<T>) supperClass.getActualTypeArguments()[0];
	}
	
	public void insert(T entity) {
		this.em.persist(entity);
	}

	public void update(T entity) {
		this.em.merge(entity);
	}

	public void delete(T entity) {
		this.em.remove(this.em.merge(entity));
	}

	public T get(Long id) {
		return this.em.find(entityClass, id);
	}

	public abstract List<T> getAll();
}
